﻿using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using EnvDTE;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Drawing;
using System.Drawing.Design;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;

namespace Adaptive.Foundation.CodeDom.Native
{
    public class VsCodeDomGenerator : ICodeGenerator
    {
        // Fields
        private bool checkedVariableNaming;
        private EventHandler ehHandlerOffsets;
        private Hashtable eventSignatures;
        private string fileName;
        private bool implementsBodyPoint;
        private ICodeGenerator innerGenerator;
        private VsCodeDomProvider provider;
        private __VSDESIGNER_VARIABLENAMING variableNaming;

        // Methods
        internal VsCodeDomGenerator(ICodeGenerator innerGenerator, VsCodeDomProvider provider)
        {
            this.innerGenerator = innerGenerator;
            this.provider = provider;
            this.implementsBodyPoint = true;
            this.ehHandlerOffsets = new EventHandler(this.GetHandlerOffsets);
        }

        public string CreateEscapedIdentifier(string value)
        {
            return this.innerGenerator.CreateEscapedIdentifier(value);
        }

        public string CreateValidIdentifier(string value)
        {
            string str = this.innerGenerator.CreateValidIdentifier(value);
            if ((str != null) && (str.Length > 0))
            {
                switch (this.VariableNaming)
                {
                    case __VSDESIGNER_VARIABLENAMING.VSDVN_Camel:
                        return (char.ToLower(str[0], CultureInfo.InvariantCulture) + str.Substring(1));

                    case __VSDESIGNER_VARIABLENAMING.VSDVN_VB:
                        return (char.ToUpper(str[0], CultureInfo.InvariantCulture) + str.Substring(1));
                }
            }
            return str;
        }

        private void EnsureHandlesClauseHookups(CodeClass vsClass, ICollection eventAttachStatements)
        {
            if (((vsClass != null) && (eventAttachStatements != null)) && ((eventAttachStatements.Count != 0) && this.provider.IsVB))
            {
                foreach (CodeAttachEventStatement statement in eventAttachStatements)
                {
                    CodeDelegateCreateExpression listener = statement.Listener as CodeDelegateCreateExpression;
                    string name = null;
                    if (listener != null)
                    {
                        name = listener.MethodName;
                        Type delegateType = (Type)statement.UserData[typeof(Delegate)];
                        if (delegateType != null)
                        {
                            CodeMemberMethod methodFromDelegate = this.GetMethodFromDelegate(name, delegateType);
                            CodeFunction function = (CodeFunction)this.VsFindMember(vsClass.Members, name, methodFromDelegate, false, (EnvDTE.vsCMElement)2);
                            if (function != null)
                            {
                                IEventHandler handler = function as IEventHandler;
                                if (handler != null)
                                {
                                    string hookupName = this.GetHookupName(statement, vsClass.Name);
                                    if (!handler.HandlesEvent(hookupName))
                                    {
                                        handler.AddHandler(hookupName);
                                        if (this.provider.FullParseHandlers == null)
                                        {
                                            this.provider.FullParseHandlers = new Hashtable();
                                        }
                                        this.provider.FullParseHandlers[hookupName] = methodFromDelegate;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        public void GenerateCodeFromCompileUnit(CodeCompileUnit e, TextWriter w, CodeGeneratorOptions o)
        {
            if (this.IsExternalWriter(w))
            {
                this.innerGenerator.GenerateCodeFromCompileUnit(e, w, o);
            }
            else
            {
                try
                {
                    foreach (System.CodeDom.CodeNamespace namespace2 in e.Namespaces)
                    {
                        this.GenerateCodeFromNamespace(namespace2, w, o);
                    }
                }
                catch (ExternalException)
                {
                    IUIService service = (IUIService)((IServiceProvider)w).GetService(typeof(IUIService));
                    if (service != null)
                    {
                        service.ShowMessage("GlobalEdit");
                    }
                }
            }
        }

        public void GenerateCodeFromExpression(CodeExpression e, TextWriter w, CodeGeneratorOptions o)
        {
            this.innerGenerator.GenerateCodeFromExpression(e, w, o);
        }

        public void GenerateCodeFromNamespace(System.CodeDom.CodeNamespace e, TextWriter w, CodeGeneratorOptions o)
        {
            if (this.IsExternalWriter(w))
            {
                this.innerGenerator.GenerateCodeFromNamespace(e, w, o);
            }
            else
            {
                foreach (CodeTypeDeclaration declaration in e.Types)
                {
                    this.GenerateCodeFromType(declaration, w, o);
                }
            }
        }

        public void GenerateCodeFromStatement(CodeStatement e, TextWriter w, CodeGeneratorOptions o)
        {
            this.innerGenerator.GenerateCodeFromStatement(e, w, o);
        }

        public void GenerateCodeFromType(CodeTypeDeclaration e, TextWriter w, CodeGeneratorOptions o)
        {
            if (this.IsExternalWriter(w))
            {
                this.innerGenerator.GenerateCodeFromType(e, w, o);
            }
            else
            {
                CodeClass vsClass = (CodeClass)e.UserData[VsCodeDomParser.VsElementKey];
                if (vsClass != null)
                {
                    ArrayList list = new ArrayList();
                    CodeTypeMemberCollection members = null;
                    ArrayList eventAttachStatements = null;
                    IVsCompoundAction service = (IVsCompoundAction)((IServiceProvider)w).GetService(typeof(IVsCompoundAction));
                    try
                    {
                        if ((service != null) && !((service.OpenCompoundAction("SerializerDesignerGeneratedCode"))>0))
                        {
                            service = null;
                        }
                        bool flag = false;
                        try
                        {
                            this.fileName = this.provider.FileName;
                            CodeTypeMember insertAfter = null;
                            members = new CodeTypeMemberCollection();
                            CodeTypeMemberCollection members2 = e.UserData[VsCodeDomParser.VsOriginalCollectionKey] as CodeTypeMemberCollection;
                            if (members2 == null)
                            {
                                goto Label_015F;
                            }
                            IEnumerator enumerator = members2.GetEnumerator();
                            this.provider.ClearGeneratedMembers();
                        Label_0123:
                            foreach (CodeTypeMember member2 in e.Members)
                            {
                                if (member2.UserData[VsCodeDomParser.VsElementKey] == null)
                                {
                                    goto Label_0123;
                                }
                                while (enumerator.MoveNext())
                                {
                                    if (enumerator.Current == member2)
                                    {
                                        goto Label_0123;
                                    }
                                    this.VsRemoveMember(vsClass, (CodeTypeMember)enumerator.Current);
                                }
                            }
                            while (enumerator.MoveNext())
                            {
                                this.VsRemoveMember(vsClass, (CodeTypeMember)enumerator.Current);
                            }
                        Label_015F:
                            flag = true;
                            this.provider.StartEdit();
                        Label_0240:
                            foreach (CodeTypeMember member3 in e.Members)
                            {
                                members.Add(member3);
                                object obj3 = member3.UserData[VsCodeDomParser.VsElementKey];
                                if (obj3 == null)
                                {
                                    if (member3 is CodeMemberField)
                                    {
                                        if (this.VsAddField(vsClass, (CodeMemberField)member3, insertAfter))
                                        {
                                            goto Label_0220;
                                        }
                                        goto Label_0240;
                                    }
                                    if (member3 is CodeMemberMethod)
                                    {
                                        if (this.VsAddMethod(vsClass, (CodeMemberMethod)member3, insertAfter) == null)
                                        {
                                            goto Label_0240;
                                        }
                                        list.Add(member3);
                                    }
                                }
                                else if ((member3 is CodeMemberMethod) && (member3.UserData[VsCodeDomParser.VsGenerateStatementsKey] != null))
                                {
                                    list.Add(member3);
                                }
                            Label_0220:
                                if (member3 is CodeMemberField)
                                {
                                    this.provider.AddGeneratedField(e, (CodeMemberField)member3);
                                }
                                insertAfter = member3;
                            }
                        }
                        finally
                        {
                            if (flag)
                            {
                                this.provider.EndEdit();
                            }
                        }
                        try
                        {
                            this.provider.StartEdit();
                            StringBuilder sb = new StringBuilder(0x100);
                        Label_049A:
                            foreach (CodeMemberMethod method in list)
                            {
                                sb.Length = 0;
                                StringWriter writer = new StringWriter(sb);
                                CodeFunction function = method.UserData[VsCodeDomParser.VsElementKey] as CodeFunction;
                                if (function == null)
                                {
                                    goto Label_049A;
                                }
                                CodeStatementCollection statements = method.Statements;
                                bool flag3 = statements.Count > 0;
                                foreach (CodeStatement statement in statements)
                                {
                                    if (!this.provider.IsVB || !(statement is CodeAttachEventStatement))
                                    {
                                        int length = sb.Length;
                                        try
                                        {
                                            this.innerGenerator.GenerateCodeFromStatement(statement, writer, o);
                                        }
                                        catch (Exception)
                                        {
                                            string str = sb.ToString(length, sb.Length - length);
                                            sb.Length = length;
                                            if (statement is CodeAssignStatement)
                                            {
                                                int index = str.IndexOf(' ');
                                                if (index != -1)
                                                {
                                                    str = str.Substring(0, index);
                                                }
                                                CodeCommentStatement statement2 = new CodeCommentStatement("TODO: FailedToGenerateCode");
                                                this.innerGenerator.GenerateCodeFromStatement(statement2, writer, o);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (eventAttachStatements == null)
                                        {
                                            eventAttachStatements = new ArrayList();
                                        }
                                        eventAttachStatements.Add(statement);
                                    }
                                    if (flag3 && (statement.UserData["_XmlCodeDomParseStmt"] == null))
                                    {
                                        flag3 = false;
                                    }
                                }
                                if ((flag3 || (statements.Count <= 0)) && ((statements.Count != 0) || !(((string)method.UserData[VsCodeDomParser.VsGenerateStatementsKey]) == VsCodeDomParser.VsGenerateStatementsKey)))
                                {
                                    goto Label_049A;
                                }
                                string text = sb.ToString();
                                method.UserData[VsCodeDomParser.VsGenerateStatementsKey] = VsCodeDomParser.VsGenerateStatementsKey;
                                this.VsReplaceChildren((CodeElement)function, text);
                                method.UserData[typeof(string)] = text;
                            }
                        }
                        finally
                        {
                            this.provider.EndEdit();
                        }
                        e.UserData[VsCodeDomParser.VsOriginalCollectionKey] = members;
                        if (eventAttachStatements != null)
                        {
                            this.EnsureHandlesClauseHookups(vsClass, eventAttachStatements);
                        }
                    }
                    finally
                    {
                        if (service != null)
                        {
                            service.CloseCompoundAction();
                        }
                    }
                    foreach (CodeMemberMethod method2 in list)
                    {
                        string str3 = method2.UserData[typeof(string)] as string;
                        if ((str3 != null) && (str3.Length > 0))
                        {
                            this.provider.AddGeneratedMethod(e, method2);
                        }
                        method2.UserData[typeof(string)] = null;
                        method2.UserData[typeof(EventHandler)] = this.ehHandlerOffsets;
                    }
                }
            }
        }

        private void GetHandlerOffsets(object sender, EventArgs e)
        {
            CodeMemberMethod method = sender as CodeMemberMethod;
            if (method != null)
            {
                object obj2;
                CodeFunction function = method.UserData[VsCodeDomParser.VsElementKey] as CodeFunction;
                TextPoint startPoint = null;
                try
                {
                    if (this.implementsBodyPoint)
                    {
                        startPoint = function.GetStartPoint((EnvDTE.vsCMPart)0x20);
                    }
                }
                catch
                {
                    this.implementsBodyPoint = false;
                }
                if (((startPoint == null) && (function is IMethodXML)) && (((IMethodXML)function).GetBodyPoint(out obj2) == 0))
                {
                    startPoint = obj2 as TextPoint;
                }
                if (startPoint != null)
                {
                    Point point2 = new Point(startPoint.LineCharOffset, startPoint.Line);
                    method.UserData[typeof(Point)] = point2;
                    method.LinePragma = new CodeLinePragma(this.FileName, point2.Y);
                }
            }
        }

        private string GetHookupName(CodeAttachEventStatement stmt, string thisName)
        {
            string eventName = stmt.Event.EventName;
            CodeExpression targetObject = stmt.Event.TargetObject;
            bool flag = true;
        Label_001A:
            if (targetObject is CodeThisReferenceExpression)
            {
                if (flag)
                {
                    eventName = thisName + "." + eventName;
                }
                return eventName;
            }
            if (targetObject is CodeFieldReferenceExpression)
            {
                flag = false;
                CodeFieldReferenceExpression expression2 = (CodeFieldReferenceExpression)targetObject;
                eventName = expression2.FieldName + "." + eventName;
                targetObject = expression2.TargetObject;
                goto Label_001A;
            }
            if (targetObject is CodePropertyReferenceExpression)
            {
                flag = false;
                CodePropertyReferenceExpression expression3 = (CodePropertyReferenceExpression)targetObject;
                eventName = expression3.PropertyName + "." + eventName;
                targetObject = expression3.TargetObject;
                goto Label_001A;
            }
            return eventName;
        }

        private CodeMemberMethod GetMethodFromDelegate(string name, Type delegateType)
        {
            CodeMemberMethod method = null;
            if (this.eventSignatures == null)
            {
                this.eventSignatures = new Hashtable();
            }
            else if (this.eventSignatures.Contains(delegateType))
            {
                method = (CodeMemberMethod)this.eventSignatures[delegateType];
            }
            if (method == null)
            {
                /*
                MethodInfo info = delegateType.GetMethod("Invoke");
                method = new CodeMemberMethod();
                CodeDomLoader.DelegateTypeInfo info2 = new CodeDomLoader.DelegateTypeInfo(delegateType);
                method.ReturnType = info2.ReturnType;
                method.Parameters.AddRange(info2.Parameters);
                this.eventSignatures[delegateType] = method;
                 */
            }
            method.Name = name;
            return method;
        }

        public string GetTypeOutput(CodeTypeReference type)
        {
            return this.innerGenerator.GetTypeOutput(type);
        }

        private bool IsExternalWriter(TextWriter w)
        {
            if (w == null)
            {
                return false;
            }
            IServiceProvider provider = w as IServiceProvider;
            if (provider != null)
            {
                return (provider.GetService(typeof(TextBuffer)) == null);
            }
            return true;
        }

        public bool IsValidIdentifier(string value)
        {
            if (this.provider.CodeModel != null)
            {
                return this.provider.CodeModel.IsValidID(value);
            }
            return this.innerGenerator.IsValidIdentifier(value);
        }

        public bool Supports(GeneratorSupport supports)
        {
            return this.innerGenerator.Supports(supports);
        }

        public void ValidateIdentifier(string value)
        {
            this.innerGenerator.ValidateIdentifier(value);
            if ((this.provider.CodeModel != null) && !this.provider.CodeModel.IsValidID(value))
            {
                throw new ArgumentException("SerializerInvalidIdentifier");
            }
        }

        private vsCMAccess VsAccessFromMemberAttributes(MemberAttributes modifiers)
        {
            modifiers &= MemberAttributes.AccessMask;
            MemberAttributes attributes = modifiers;
            if (attributes <= MemberAttributes.Family)
            {
                switch (attributes)
                {
                    case MemberAttributes.Assembly:
                        return (EnvDTE.vsCMAccess)4;

                    case MemberAttributes.Family:
                        return (EnvDTE.vsCMAccess)8;
                }
            }
            else if (attributes != MemberAttributes.FamilyOrAssembly)
            {
                if (attributes == MemberAttributes.Private)
                {
                    return (EnvDTE.vsCMAccess)2;
                }
                if (attributes == MemberAttributes.Public)
                {
                    return (EnvDTE.vsCMAccess)1;
                }
            }
            else
            {
                return (EnvDTE.vsCMAccess)12;
            }
            return (vsCMAccess)0x20;
        }

        private bool VsAddField(CodeClass vsParentClass, CodeMemberField newField, CodeTypeMember insertAfter)
        {
            if (this.VsFindMember(vsParentClass.Members, newField.Name, null, false, (EnvDTE.vsCMElement)3) != null)
            {
                return false;
            }
            try
            {
                object name;
                vsCMAccess access = this.VsAccessFromMemberAttributes(newField.Attributes);
                if (this.provider.IsVB)
                {
                    Type c = this.provider.TypeLoader.GetType(this.VsFormatType(newField.Type.BaseType), false);
                    if ((c != null) && typeof(IComponent).IsAssignableFrom(c))
                    {
                        access |= (EnvDTE.vsCMAccess)0x80;
                    }
                }
                if (insertAfter != null)
                {
                    if (insertAfter is CodeTypeConstructor)
                    {
                        name = vsParentClass.Name;
                    }
                    else
                    {
                        name = insertAfter.Name;
                    }
                }
                else
                {
                    name = 0;
                }
                CodeVariable variable = vsParentClass.AddVariable(newField.Name, this.VsFormatType(newField.Type.BaseType), name, access, 0);
                newField.UserData[VsCodeDomParser.VsElementKey] = variable;
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        private CodeElement VsAddMethod(CodeClass vsParentClass, CodeMemberMethod newMethod, CodeTypeMember insertAfter)
        {
            CodeFunction function = (CodeFunction)this.VsFindMember(vsParentClass.Members, newMethod.Name, newMethod, true, (EnvDTE.vsCMElement)2);
            if (function == null)
            {
                try
                {
                    vsCMFunction function2;
                    string fullName = this.VsFormatType(newMethod.ReturnType.BaseType);
                    if (fullName == null)
                    {
                        fullName = typeof(void).FullName;
                    }
                    if (this.provider.IsVB && (fullName == typeof(void).FullName))
                    {
                        function2 = (vsCMFunction)0x40;
                    }
                    else
                    {
                        function2 = (vsCMFunction)0x80;
                    }
                    function = vsParentClass.AddFunction(newMethod.Name, function2, fullName, (insertAfter == null) ? ((object)0) : ((object)insertAfter.Name), this.VsAccessFromMemberAttributes(newMethod.Attributes), 0);
                }
                catch
                {
                }
                if (function == null)
                {
                    return null;
                }
                newMethod.UserData[VsCodeDomParser.VsElementKey] = function;
                foreach (CodeParameterDeclarationExpression expression in newMethod.Parameters)
                {
                    CodeParameter parameter = function.AddParameter(expression.Name, this.VsFormatType(expression.Type.BaseType), -1);
                    if ((parameter != null) && (parameter is IParameterKind))
                    {
                        IParameterKind kind = (IParameterKind)parameter;
                        for (CodeTypeReference reference = expression.Type; reference.ArrayRank > 0; reference = reference.ArrayElementType)
                        {
                            kind.SetParameterArrayDimensions(reference.ArrayRank);
                        }
                        switch (expression.Direction)
                        {
                            case FieldDirection.Out:
                                {
                                    kind.SetParameterPassingMode(PARAMETER_PASSING_MODE.cmParameterTypeOut);
                                    continue;
                                }
                            case FieldDirection.Ref:
                                {
                                    kind.SetParameterPassingMode(PARAMETER_PASSING_MODE.cmParameterTypeInOut);
                                    continue;
                                }
                        }
                    }
                }
                IEventHandler handler = function as IEventHandler;
                if ((handler != null) && this.provider.IsVB)
                {
                    string str2 = (string)newMethod.UserData[typeof(IComponent)];
                    string bstrEventName = (string)newMethod.UserData[typeof(EventDescriptor)];
                    if ((str2 != null) && (bstrEventName != null))
                    {
                        bstrEventName = str2 + "." + bstrEventName;
                        if (!handler.HandlesEvent(bstrEventName))
                        {
                            handler.AddHandler(bstrEventName);
                            if (this.provider.FullParseHandlers == null)
                            {
                                this.provider.FullParseHandlers = new Hashtable();
                            }
                            this.provider.FullParseHandlers[bstrEventName] = newMethod;
                        }
                        newMethod.UserData[typeof(IComponent)] = null;
                        newMethod.UserData[typeof(EventDescriptor)] = null;
                    }
                }
            }
            return (CodeElement)function;
        }

        private CodeElement VsFindMember(CodeElements elements, string name, CodeMemberMethod newMethod, bool checkMethodReturnType, vsCMElement elementKind)
        {
            CodeElement codeElement = null;
            try
            {
                CodeElements_Internal internal2 = elements as CodeElements_Internal;
                if (internal2 != null)
                {
                    if (( (internal2.Item(name, out codeElement)) < 0))
                    {
                        codeElement = null;
                    }
                }
                else
                {
                    codeElement = elements.Item(name);
                }
            }
            catch
            {
            }
            if (codeElement != null)
            {
                if (codeElement.Kind == elementKind)
                {
                    switch (elementKind)
                    {
                        case (EnvDTE.vsCMElement)2:
                            if (newMethod != null)
                            {
                                codeElement = this.VsFindMemberMethod((CodeFunction)codeElement, newMethod, checkMethodReturnType);
                            }
                            return codeElement;

                        case (EnvDTE.vsCMElement)3:
                            return codeElement;
                    }
                    return codeElement;
                }
                foreach (CodeElement element2 in elements)
                {
                    if (element2.Kind != elementKind)
                    {
                        continue;
                    }
                    if ((element2.Kind == (EnvDTE.vsCMElement)3) && (((CodeVariable)element2).Name == name))
                    {
                        return element2;
                    }
                    if (element2.Kind == (EnvDTE.vsCMElement)2)
                    {
                        CodeElement element3 = this.VsFindMemberMethod((CodeFunction)element2, newMethod, checkMethodReturnType);
                        if (element3 == null)
                        {
                            continue;
                        }
                        return element3;
                    }
                }
            }
            return null;
        }

        private CodeElement VsFindMemberMethod(CodeFunction vsFoundElem, CodeMemberMethod newMethod, bool checkMethodReturnType)
        {
            if (vsFoundElem.IsOverloaded)
            {
                IEnumerator enumerator = vsFoundElem.Overloads.GetEnumerator();
                vsFoundElem = null;
                while (enumerator.MoveNext())
                {
                    if ((newMethod != null) && this.VsMethodEquals((CodeFunction)enumerator.Current, newMethod, checkMethodReturnType))
                    {
                        vsFoundElem = (CodeFunction)enumerator.Current;
                    }
                }
            }
            else if ((newMethod == null) || !this.VsMethodEquals(vsFoundElem, newMethod, checkMethodReturnType))
            {
                vsFoundElem = null;
            }
            return (CodeElement)vsFoundElem;
        }

        private string VsFormatType(string fullTypeName)
        {
            return fullTypeName.Replace('+', '.');
        }

        private bool VsMethodEquals(CodeFunction vsMethod, CodeMemberMethod codeMethod, bool checkMethodAccess)
        {
            string fullName = vsMethod.Type.AsFullName;
            if ((fullName == null) || (fullName.Length == 0))
            {
                fullName = typeof(void).FullName;
            }
            CodeElements elements = vsMethod.Parameters;
            if ((((vsMethod.Name != codeMethod.Name) || (fullName != this.VsFormatType(codeMethod.ReturnType.BaseType))) || (elements.Count != codeMethod.Parameters.Count)) || (checkMethodAccess && (vsMethod.Access != this.VsAccessFromMemberAttributes(codeMethod.Attributes))))
            {
                return false;
            }
            IEnumerator enumerator = elements.GetEnumerator();
            IEnumerator enumerator2 = codeMethod.Parameters.GetEnumerator();
            bool flag2 = true;
            int count = codeMethod.Parameters.Count;
            while ((flag2 && enumerator.MoveNext()) && enumerator2.MoveNext())
            {
                try
                {
                    CodeParameter current = (CodeParameter)enumerator.Current;
                    CodeParameterDeclarationExpression expression = (CodeParameterDeclarationExpression)enumerator2.Current;
                    CodeTypeRef ref2 = current.Type;
                    if ((int)ref2.TypeKind == 2)
                    {
                        while ((int)ref2.TypeKind == 2)
                        {
                            ref2 = ref2.ElementType;
                        }
                    }
                    flag2 = ref2.AsFullName == this.VsFormatType(expression.Type.BaseType);
                }
                catch
                {
                    flag2 = false;
                }
                count--;
            }
            return (flag2 && (count == 0));
        }

        private void VsRemoveMember(CodeClass vsClass, CodeTypeMember member)
        {
            CodeElement element = member.UserData[VsCodeDomParser.VsElementKey] as CodeElement;
            vsClass.RemoveMember(element);
        }

        private void VsReplaceChildren(CodeElement vsElement, string text)
        {
            EditPoint point = null;
            TextPoint endPoint = null;
            try
            {
                point = vsElement.GetStartPoint((EnvDTE.vsCMPart)0x10).CreateEditPoint();
                endPoint = vsElement.GetEndPoint((EnvDTE.vsCMPart)0x10);
            }
            catch
            {
            }
            if ((point != null) && (endPoint != null))
            {
                point.ReplaceText(endPoint, text + "\r\n", 8);
            }
        }

        // Properties
        private string FileName
        {
            get
            {
                return this.fileName;
            }
        }

        private __VSDESIGNER_VARIABLENAMING VariableNaming
        {
            get
            {
                if (!this.checkedVariableNaming)
                {
                    this.checkedVariableNaming = true;
                    this.variableNaming = __VSDESIGNER_VARIABLENAMING.VSDVN_Camel;
                    try
                    {
                        object obj2;
                        IVsHierarchy hierarchy = this.provider.Hierarchy;
                        if ((hierarchy != null) && ((hierarchy.GetProperty(-2, -2060, out obj2)) > 0))
                        {
                            this.variableNaming = (__VSDESIGNER_VARIABLENAMING)Enum.ToObject(typeof(__VSDESIGNER_VARIABLENAMING), obj2);
                        }
                    }
                    catch
                    {
                    }
                }
                return this.variableNaming;
            }
        }

        // Nested Types
        [ComImport, InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("0CFBC2B5-0D4E-11D3-8997-00C04F688DDE")]
        private interface CodeElements_Internal
        {
            void NewEnum_PlaceHolder_DontCallMe();
            object DTE_PlaceHolder_DontCallMe();
            object Parent_PlaceHolder_DontCallMe();
            [PreserveSig]
            int Item(object index, out CodeElement codeElement);
            void Count_PlaceHolder_DontCallMe();
            void Reserved1_PlaceHolder_DontCallMe();
            void CreateUniqueID_PlaceHolder_DontCallMe();
        }
    }
}
